package com.example.administrator.im.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

/**
 * create:2018/9/23 19:35
 * author: 姚鸿杰
 **/
public class PullDownImg extends NestedScrollView {
    public PullDownImg(Context context) {
        this(context, null);
    }

    public PullDownImg(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private View imgView;       //图片控件
    //    private View vg_container;
    private int imgViewWidth;   //缩放后的宽度
    private int imgViewHeight;  //高度

    private float firstPosition;//记录第一次按下的位置
    private boolean isScrolling;//是否正在缩放
    private float mScrollRate = 0.4f;//缩放系数，缩放系数越大，变化的越大
    private float mReplyRate = 0.9f;//回调系数，越大，回调越慢


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    private void init() {
        setOverScrollMode(OVER_SCROLL_NEVER);
        if (getChildAt(0) != null) {
            ViewGroup vg = (ViewGroup) getChildAt(0);
            if (vg.getChildAt(0) != null) {
                imgView = vg.getChildAt(0);

            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (imgViewWidth <= 0 || imgViewHeight <= 0) {
            imgViewWidth = imgView.getMeasuredWidth();
            imgViewHeight = imgView.getMeasuredHeight();
        }

        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                //手指离开后恢复图片
                isScrolling = false;
                replyImage();
                break;
            case MotionEvent.ACTION_MOVE:

                if (!isScrolling) {
                    if (getScrollY() == 0) {
                        firstPosition = ev.getY();// 滚动到顶部时记录位置，否则正常返回
                    } else {
                        break;
                    }
                }
                int distance = (int) ((ev.getY() - firstPosition) * mScrollRate); // 滚动距离乘以一个系数
                if (distance < 0) { // 当前位置比记录位置要小，正常返回
                    break;
                }

                // 处理放大
                isScrolling = true;
                setZoom(distance);
                return true; // 返回true表示已经完成触摸事件，不再处理
        }
        return super.onTouchEvent(ev);
    }

    private float downY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

            case MotionEvent.ACTION_DOWN:
                downY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float y = ev.getY();
                if (y > downY && getScrollY() == 0) {
                    return true;
                }
                break;
        }

        return super.onInterceptTouchEvent(ev);
    }

    private void setZoom(float distance) {
        if (imgViewWidth <= 0 || imgViewHeight <= 0) {
            return;
        }

        ViewGroup.LayoutParams lp = imgView.getLayoutParams();
        lp.height = (int) (imgViewHeight + distance);
        imgView.setLayoutParams(lp);
    }

    private void replyImage() {
        float distance = imgView.getMeasuredHeight() - imgViewHeight;
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(distance, 0f).setDuration((long) (distance * mReplyRate));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setZoom((Float) animation.getAnimatedValue());
            }
        });
        valueAnimator.start();
    }

}
